package com.seu.me.test.date;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author liangfeihu
 * @since 2019/11/5 17:54
 */
public class DateTest01 {
    public static void main(String[] args) {
        String yyyyMMddHHmm = formatDateTime(new Date(), "yyyyMMddHHmm");
        System.out.println(yyyyMMddHHmm);
    }

    /**
     * 按照指定格式生成时间字符串
     */
    public static String formatDateTime(LocalDateTime date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return date.format(formatter);
    }

    /**
     * 按照指定格式生成时间字符串
     */
    public static String formatDateTime(Date date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return toLocalDateTime(date).format(formatter);
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }
}
